package main.model.json;

import java.lang.reflect.Modifier;
import java.lang.reflect.Type;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.stream.JsonReader;
/**
 * class for conversion json <-> object
 * @author Tomek Wagner
 *
 */
public class JsonSerializer
{
	/**
	 * Deserialize json to object
	 * @param reader - source of json string, in this case JsonReader
	 * @param classType - type of object`s class
	 * @return deserialized object of specific type
	 */
	public static <T> T deserialize(JsonReader reader, Type classType)
	{
		Gson gson = new Gson();
		T obj = gson.fromJson(reader, classType);	
		return obj;
	}
	
	/**
	 * Serialize object to String
	 * @param object - object of specific type
	 * @return - serialized json string
	 */
	public static <T> String serialize(T object)
	{
		 Gson gson = new GsonBuilder()
	        .excludeFieldsWithModifiers(Modifier.TRANSIENT).setPrettyPrinting()
	        .create();
		 return gson.toJson(object);	
	}
}
